//
//  AppDelegate.m
//  HealthA段
//
//  Created by dllo on 15/6/3.
//  Copyright (c) 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "AppDelegate.h"
#import "NewsViewController.h"
#import "HosptialViewController.h"
#import "KnowledgeViewController.h"
#import "FoodViewController.h"
#import "ZWIntroductionViewController.h"
#import "UMSocial.h"
#import "Singleton.h"

#import "Reachability.h"
#import <SystemConfiguration/SystemConfiguration.h>

#define WIDTH self.window.frame.size.width
#define HEIGHT self.window.frame.size.height
@interface AppDelegate ()
@property (nonatomic, strong) ZWIntroductionViewController *introductionView;
@property(nonatomic, assign)BOOL isFirstStart;
@property(nonatomic, retain)UITabBarController *myTabBar;
@property(nonatomic,retain)UIView *wellcomView;

@property(nonatomic,retain)Reachability *res;
@end

@implementation AppDelegate
- (instancetype)init{
    self = [super init];
    if (self) {
        [self isFirstStartProject];
    }
    return self;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //添加网络变化的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(change:) name:kReachabilityChangedNotification object:nil];
    self.res = [Reachability reachabilityWithHostName:@"http://www.baidu.com"];
    //开始坚挺网络请求变化
    [self.res startNotifier];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    if (self.isFirstStart) {
        NSLog(@"第一次启动");
        NSArray *coverImageNames = @[@"1.jpg", @"4.jpg", @"2.jpg"];
        self.introductionView = [[ZWIntroductionViewController alloc] initWithCoverImageNames:coverImageNames backgroundImageNames:nil];
        [self.window addSubview:self.introductionView.view];
        __weak AppDelegate *weakSelf = self;
        self.introductionView.didSelectedEnter = ^() {
            [weakSelf.introductionView.view removeFromSuperview];
            weakSelf.introductionView = nil;
            self.myTabBar = [[UITabBarController alloc] init];
            weakSelf.window.rootViewController = self.myTabBar;
            [self addActivityView];
            [self creatView];
        };
    }else{
        NSLog(@"不是第一次启动");
        self.myTabBar = [[UITabBarController alloc] init];
        self.window.rootViewController = self.myTabBar;
        [self addActivityView];
        [self creatView];
    }
    [_window release];
    [UMSocialData setAppKey:@"55406a3ce0f55a7bd5002d90"];
    [Singleton shareSingleton].delegateAlph = self;
    [Singleton shareSingleton].delegateActive = self;
    return YES;
 }

- (void)dealloc
{
    [_res release];
    [_window release];
    [super dealloc];
}
//当网络变化时，都会触发这个方法
-(void)change:(NSNotificationCenter *)notify
{
    Reachability *r=[Reachability reachabilityWithHostName:@"http://www.baidu.com"];
    switch ([r currentReachabilityStatus]) {
        case NotReachable:
            NSLog(@"not work");
            break;
        case ReachableViaWiFi:
            NSLog(@"wifi");
            break;
        case ReachableViaWWAN:{
            NSLog(@"wan");
        }
            break;
        default:
            break;
    }
    
    Reachability *wifi = [Reachability reachabilityForLocalWiFi];
    
    Reachability *conn = [Reachability reachabilityForInternetConnection];
    
    if ([wifi currentReachabilityStatus] != NotReachable) {
        //        UIAlertView *aler = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"当前网络为WIFI，请放心使用" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        //            [aler show];
        //            [aler release];
        self.wellcomView.hidden = YES;
//        NSLog(@"WIFI");
    }else if ([conn currentReachabilityStatus] != NotReachable)
    {
        UIAlertView *aler = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"当前网络为手机自带网络，注意流量" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [aler show];
        [aler release];
    }else
    {
        UIAlertView *aler = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"网络连接有问题，请检查连接是否正确" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [aler show];
        [aler release];
        
        UIView *vie = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.window.frame.size.width   ,self.window.frame.size.height)];
        vie.backgroundColor = [UIColor whiteColor];
        [self.window bringSubviewToFront:vie];
        [vie release];
    }

}
- (void)creatView{
    NewsViewController *newVC = [[NewsViewController alloc] init];
    UINavigationController *newNA = [[UINavigationController alloc] initWithRootViewController:newVC];
    newNA.navigationBar.barTintColor = [UIColor colorWithRed:69/256.0 green:188/256.0 blue:153/256.0 alpha:1];
    newNA.navigationBar.translucent = NO;
    newNA.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"健康资讯" image:[UIImage imageNamed:@"zixun"] tag:1001];
    
    HosptialViewController *hosptialVC = [[HosptialViewController alloc] init];
    UINavigationController *hosNA = [[UINavigationController alloc] initWithRootViewController:hosptialVC];
    hosNA.navigationBar.barTintColor = [UIColor colorWithRed:69/256.0 green:188/256.0 blue:153/256.0 alpha:1];
    hosNA.navigationBar.translucent = NO;
    hosptialVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"健康检查" image:[UIImage imageNamed:@"yiliao"] tag:1002];
    
    KnowledgeViewController *knowVC = [[KnowledgeViewController alloc] init];
    UINavigationController *knowNA = [[UINavigationController alloc] initWithRootViewController:knowVC];
    knowNA.navigationBar.barTintColor = [UIColor colorWithRed:69/256.0 green:188/256.0 blue:153/256.0 alpha:1];
    knowNA.navigationBar.translucent = NO;
    knowVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"健康知识" image:[UIImage imageNamed:@"changshi"] tag:1003];
    
    FoodViewController *foodVC = [[FoodViewController alloc]init];
    UINavigationController *foodNA = [[UINavigationController alloc] initWithRootViewController:foodVC];
    foodNA.navigationBar.barTintColor = [UIColor colorWithRed:69/256.0 green:188/256.0 blue:153/256.0 alpha:1];
    foodNA.navigationBar.translucent = NO;
    foodVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"健康食谱" image:[UIImage imageNamed:@"yaopin"] tag:1004];
    
    self.myTabBar.viewControllers = @[newNA,knowNA,hosNA,foodNA];
    self.myTabBar.tabBar.translucent = NO;
    self.myTabBar.tabBar.barTintColor = [UIColor colorWithRed:69/256.0 green:188/256.0 blue:153/256.0 alpha:1];
 }


- (void)isFirstStartProject
{
    NSFileManager *manager = [NSFileManager defaultManager];
    //获取沙盒路径
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath = [sandBox lastObject];
    //拼接文件路径
    NSString *filePath = [sandBoxPath stringByAppendingPathComponent:@"第一次运行"];
    if (![manager fileExistsAtPath:filePath]) {
        self.isFirstStart = YES;
        //第一次启动之后创建文件
//        NSLog(@"%@",filePath);
        [manager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    }else{
        self.isFirstStart = NO;
    }
    
}

- (void)changeNight
{
    self.window.alpha = 0.5;
    NSLog(@"fuck");
}

- (void)changeDay
{
    self.window.alpha = 1;
}


- (void)addActivityView
{

    self.wellcomView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height - 49 - 64)];
    [self.window addSubview:_wellcomView];
//    self.wellcomView.backgroundColor = [UIColor whiteColor];
    [self.window bringSubviewToFront:_wellcomView];
    UIView *activityView = [[UIView alloc] initWithFrame:CGRectMake((WIDTH-100)/2, HEIGHT*(0.45), 100, 100)];
    activityView.layer.cornerRadius = 15;
    [self.wellcomView addSubview:activityView];
    activityView.backgroundColor = [UIColor blackColor];
    activityView.alpha = 0.5;
    
    UILabel *actitiLabel = [[UILabel alloc] initWithFrame:CGRectMake((WIDTH-100)/2, HEIGHT*(0.55), 100, 20)];
    [self.wellcomView addSubview:actitiLabel];
    actitiLabel.text = @"请稍等";
    actitiLabel.textColor = [UIColor whiteColor];
    actitiLabel.textAlignment = NSTextAlignmentCenter;
    actitiLabel.font = [UIFont systemFontOfSize:15];
    
    UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake((WIDTH-100)/2,HEIGHT/4, 100, 100)];
    [activity setCenter:CGPointMake(WIDTH / 2, HEIGHT/2)]; // 指定进度轮中心点
    [activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.wellcomView addSubview:activity];
    [activity startAnimating];
    
}

- (void)activityHiddenYes
{
    self.wellcomView.hidden = YES;
}

- (void)activityHiddenNO
{
    self.wellcomView.hidden = NO;
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
